package com.itheima.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.project.constant.ShopConstant;
import com.itheima.project.mapper.StoreMapper;
import com.itheima.project.pojo.Store;
import com.itheima.project.service.IStoreService;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.EmptyUtil;
import com.itheima.project.vo.StoreVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName StoreServiceImpl
 * @Description 门店service层实现类
 */
@Service
@Slf4j
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {
    @Override
    public Page<Store> findStoreVoPage(StoreVo storeVo, int pageNum, int pageSize) {
        //分页条件构建
        Page<Store> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
        //按门店名称查询
        if (!EmptyUtil.isNullOrEmpty(storeVo.getStoreName())) {
            queryWrapper.lambda().likeRight(Store::getStoreName,storeVo.getStoreName());
        }
        //按门店状态查询
        if (!EmptyUtil.isNullOrEmpty(storeVo.getEnableFlag())) {
            queryWrapper.lambda().eq(Store::getEnableFlag,storeVo.getEnableFlag());
        }
        queryWrapper.lambda().orderByDesc(Store::getCreatedTime);
        return page(page, queryWrapper);
    }
    @Override
    public Store createStore(StoreVo storeVo) {
        Store store = BeanConv.toBean(storeVo, Store.class);
        boolean flag = save(store);
        if (flag){
            return store;
        }
        return null;
    }
    @Override
    public Boolean updateStore(StoreVo storeVo) {
        Store store = BeanConv.toBean(storeVo, Store.class);
        return updateById(store);
    }
    @Override
    public Boolean deleteStore(List<Long> checkedIds) {
        return removeByIds(checkedIds);
    }
    @Override
    public List<Store> findStoreVoList() {
        QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Store::getEnableFlag, ShopConstant.YES);
        return list(queryWrapper);
    }
}
